﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Windows.Forms;
using System.Threading;

namespace MyQQ.Service
{
    public partial class Service : Form
    {

        public Service()
        {
            InitializeComponent();
            namePass["123"] = "123";
            namePass["456"] = "456";
            Net.NetReciver.Listener.MessageArrived += GetInfo;
            this.textBox1.Text = Net.NetReciver.ip + ":" + Net.NetReciver.port.ToString();
            Task.Run((Action)checkActive);
        }

        private const int Time = 300000;//心跳测试用户状态变化
        private Dictionary<string, string> namePass = new Dictionary<string, string>();//存储用户名和密码
        private List<string> nameActive = new List<string>();//存储之后用户时间状态
        private List<string> tempActive = new List<string>();//存储当前时刻用户状态变化
        private List<string> nameInActive =new List<string>();

        private Dictionary<string, Message.IpMessage> nameIp = new Dictionary<string, Message.IpMessage>();//存储用户Ip

        private void GetInfo(Message.Message msg)
        {
            switch (msg.Type)
            {
                case Message.MessageType.Register:
                    {
                        responRegister(msg.Register, msg.IP);
                        break;
                    }
                case Message.MessageType.Login:
                    {
                        responLogin(msg.Login, msg.IP);
                        break;
                    }
                case Message.MessageType.Message:
                    {
                        responMessage(msg);
                        break;
                    }
                case Message.MessageType.IsActive:
                    {
                        if (msg.IsLive == true)
                        {
                            this.tempActive.Add(msg.Towho.Name);
                        }
                        break;
                    }
            }
        }

        private void sendToOne(Message.Message msg, string name)
        {
            if(nameIp[name]!=null)
            {
                Net.NetSender.ServiceIp = nameIp[name].IP;
                Net.NetSender.ServicePort = nameIp[name].Port;

                Net.NetSender.Sender.SendMessage(msg);
            }
        }

        private void responRegister(Message.RegisterMessage rmsg,Message.IpMessage ip)
        {
           string name=rmsg.Name;
           string pass=rmsg.Password;

           Message.Message msg = new Message.Message(); msg.Type = Message.MessageType.Register;
           msg.Register = new Message.RegisterMessage(name, pass);
           if (!namePass.Keys.ToList().Contains(name))

           {
               msg.Register.success = true;
               namePass[name] = pass;
               nameIp[name] = ip;
               sendToOne(msg, name);
           }
           else
           {
               Message.IpMessage temp = nameIp[name];
               msg.Register.success = false;
               nameIp[name] = ip;
               sendToOne(msg, name);
               nameIp[name] = temp;
           }
           
                        
        }

        private void responLogin(Message.LoginMessage lmsg, Message.IpMessage ip)
        {
            string name = lmsg.Name;
            string pass = lmsg.Password;

            Message.Message msg = new Message.Message();
            msg.Type = Message.MessageType.Login;
            msg.Login = new Message.LoginMessage(name, pass);
            if (namePass[name] == pass)
            {
                msg.Login.success = true;
                //namePass[name] = pass;
                nameIp[name] = ip;
                msg.ActiveName = nameActive;
                msg.ActiveName.Remove(name);
                msg.InActiveName = nameInActive;
                msg.InActiveName.Remove(name);
                sendToOne(msg, name);
                //若名称不存在则添加进去
                if (!nameActive.Contains(name))
                {
                    nameActive.Add(name);
                }
                tempActive.Remove(name);
                nameInActive.Remove(name);
            }
            else
            {
                Message.IpMessage temp = nameIp[name];
                msg.Login.success = false;
                nameIp[name] = ip;
                sendToOne(msg, name);
                nameIp[name] = temp;
            }
        }

        private void responMessage(Message.Message msg)
        {
            sendToOne(msg, msg.Towho.Name);
        }

        private void checkActive()
        {
            for (; ; )
            {
                foreach (var r in nameActive)
                {
                    Message.Message msg = new Message.Message();
                    msg.Type = Message.MessageType.IsActive;
                    msg.IsLive = false;
                    msg.Towho = new Message.ToWhoMessage(r);
                    sendToOne(msg, r);
                }

                Thread.Sleep(Time / 10);//挂起线程

                this.nameActive = this.tempActive;
                this.tempActive = new List<string>(32);
                this.nameInActive = (from r in namePass.Keys.ToList()
                                     where !nameActive.Contains(r)
                                     select r).ToList();
                Thread.Sleep(Time);
            }
            
        }


    }
}
